package com.example.managemyself.api.controller;

import com.example.managemyself.api.entity.User;
import com.example.managemyself.api.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
 * 公共API控制器
 * 处理Android应用期望的特定路径端点
 */
@RestController
@RequestMapping("/api")
@CrossOrigin(origins = "*")
public class PublicApiController {
    
    @Autowired
    private UserService userService;
    
    /**
     * 用户登出
     */
    @PostMapping("/logout")
    public ResponseEntity<?> logoutUser() {
        return ResponseEntity.ok(Map.of("message", "登出成功"));
    }
    
    /**
     * 获取当前用户信息
     */
    @GetMapping("/user")
    public ResponseEntity<?> getCurrentUserInfo(@RequestParam(required = false) Long userId) {
        try {
            if (userId == null) {
                return ResponseEntity.badRequest().body(Map.of("error", "用户ID不能为空"));
            }
            
            Optional<User> userOpt = userService.getUserById(userId);
            if (userOpt.isPresent()) {
                User user = userOpt.get();
                Map<String, Object> response = new HashMap<>();
                response.put("id", user.getId());
                response.put("username", user.getUsername());
                response.put("email", user.getEmail());
                response.put("role", user.getRole().name());
                response.put("fullName", user.getFullName());
                response.put("phone", user.getPhone());
                response.put("avatarUrl", user.getAvatarUrl());
                return ResponseEntity.ok(response);
            } else {
                return ResponseEntity.notFound().build();
            }
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(Map.of("error", e.getMessage()));
        }
    }
    
    /**
     * 重置密码
     */
    @PostMapping("/reset-password")
    public ResponseEntity<?> resetPassword(@RequestBody Map<String, String> request) {
        String email = request.get("email");
        if (email == null || email.trim().isEmpty()) {
            return ResponseEntity.badRequest().body(Map.of("error", "邮箱不能为空"));
        }
        
        try {
            return ResponseEntity.ok(Map.of("message", "重置密码邮件已发送"));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(Map.of("error", "重置密码失败：" + e.getMessage()));
        }
    }
    
    /**
     * 验证验证码
     */
    @PostMapping("/verify-code")
    public ResponseEntity<?> verifyCode(@RequestBody Map<String, String> request) {
        String email = request.get("email");
        String code = request.get("code");
        
        if (email == null || code == null) {
            return ResponseEntity.badRequest().body(Map.of("error", "邮箱和验证码不能为空"));
        }
        
        try {
            return ResponseEntity.ok(Map.of("message", "验证码验证成功"));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(Map.of("error", "验证码验证失败：" + e.getMessage()));
        }
    }
} 